import { request, encapsulation } from './request'
import { getToken } from '@/network/bookManager.js'
/*
 *@description: 登入方法
 *@modifyContent:
 *@author: chendi
 *@date: 2020-08-17 15:28:24
 */
export function login(option) {
  return encapsulation({
    url: '/api/tcb/databasequery',
    method: 'post',
    data: {
      query: `db.collection("boxAdministrators").where({adAccount :"${option.username}",adPassword:"${option.password}"}).limit(1).get()`
    }
  })
}
/*
 *@functionName: 获取所有用户信息
 *@params1: 参数1
 *@description:
 *@author: chendi
 *@date: 2020-09-03 21:22:07
 *@version: V1.0.5
 */
export async function queryUserAll(option) {
  let token = await getToken()
  return request({
    url: `/api/tcb/invokecloudfunction?&access_token=${token.access_token}&env=zhuma-mbnti&name=queryUser`,
    method: `post`,
    data: JSON.stringify(option)
  })
}
/*
 *@functionName: 获取用户总数
 *@params1: 参数1
 *@description:
 *@author: chendi
 *@date: 2020-09-04 19:58:17
 *@version: V1.0.5
*/
export async function queryUserToatl(option) {
  let token = await getToken()
  return request({
    url: `/api/tcb/invokecloudfunction?&access_token=${token.access_token}&env=zhuma-mbnti&name=queryUserTotal`,
    method: `post`,
    data: JSON.stringify(option)
  })
}
/*
 *@functionName: 获取所有角色信息
 *@params1: 参数1
 *@description:
 *@author: chendi
 *@date: 2020-09-04 08:51:57
 *@version: V1.0.5
 */
export function queryRole(option) {
  return encapsulation({
    url: '/api/tcb/databasequery',
    method: 'post',
    data: {
      query: `db.collection("boxRole")
      .where({
        ${
          option.roleStatus != undefined
            ? `roleStatus:${option.roleStatus}`
            : ``
        }
      })
      .get()`
    }
  })
}

/*
 *@functionName:  修改用户
 *@params1: 参数1
 *@description:
 *@author: chendi
 *@date: 2020-09-04 09:54:31
 *@version: V1.0.5
 */
export function updateUserById(option) {
  return encapsulation({
    url: '/api/tcb/databaseupdate',
    method: 'post',
    data: {
      query: `db.collection("boxAdministrators")
      .where({
        _id:'${option._id}'
      }).update({
        data:{
          ${option.adAccount ? `adAccount:'${option.adAccount}',` : ``}
          ${option.adPassword ? `adPassword:'${option.adPassword}',` : ``}
          ${option.adStatus != undefined ? `adStatus:${option.adStatus},` : ``}
        }
      })`
    }
  })
}
/*
 *@functionName: 修改角色与账户关联表
 *@params1: 参数1
 *@description:
 *@author: chendi
 *@date: 2020-09-04 16:34:54
 *@version: V1.0.5
 */
export function updateUserRoleById(option) {
  return encapsulation({
    url: '/api/tcb/databaseupdate',
    method: 'post',
    data: {
      query: `db.collection("boxUserRole")
      .where({
        _id:'${option._id}'
      }).update({
        data:{
          ${option.roleId ? `roleId:'${option.roleId}',` : ``}
          ${option.uid ? `uid:'${option.uid}',` : ``}
        }
      })`
    }
  })
}

/*
 *@functionName: 查询用户
 *@params1: 参数1
 *@description:
 *@author: chendi
 *@date: 2020-09-04 17:43:40
 *@version: V1.0.5
 */
export function queryUser(option) {
  return encapsulation({
    url: '/api/tcb/databasequery',
    method: 'post',
    data: {
      query: `db.collection("boxAdministrators")
       .where({
         ${option._id ? `roleStatus:'${option._id}',` : ``}
         ${option.adAccount ? `adAccount:'${option.adAccount}',` : ``}
         ${option.adPassword ? `adPassword:'${option.adPassword}',` : ``}
       })
       .get()`
    }
  })
}
/*
 *@functionName: 添加用户
 *@params1: 参数1
 *@description:
 *@author: chendi
 *@date: 2020-09-04 18:55:39
 *@version: V1.0.5
*/
export function addUser(option) {
  return encapsulation({
    url: '/api/tcb/databaseadd',
    method: 'post',
    data: {
      query: `db.collection('boxAdministrators')
       .add({
         data: [{
          adAccount: '${option.adAccount}',
          adPassword: '${option.adPassword}',
          adStatus: ${option.adStatus},
           }]
       })`
    }
  })
}
/*
 *@functionName: 增加 角色与账户关联信息
 *@params1: 参数1
 *@description:
 *@author: chendi
 *@date: 2020-09-04 18:56:30
 *@version: V1.0.5
*/
export function addUserRole(option) {
  return encapsulation({
    url: '/api/tcb/databaseadd',
    method: 'post',
    data: {
      query: `db.collection('boxUserRole')
       .add({
         data: [{
          roleId: '${option.roleId}',
          uid: '${option.uid}'
           }]
       })`
    }
  })
}
/*
 *@functionName: 删除角色 与账户 关联
 *@params1: 参数1
 *@description:
 *@author: chendi
 *@date: 2020-09-04 22:06:42
 *@version: V1.0.5
*/
export function removeUserRole(option) {
  return encapsulation({
    url: '/api/tcb/databasedelete',
    method: 'post',
    data: {
      query: `db.collection('boxUserRole')
      .where({
        uid:'${option}'
      }).remove()`
    }
  })
}
/*
 *@functionName: 删除 账户
 *@params1: 参数1
 *@description:
 *@author: chendi
 *@date: 2020-09-04 22:08:47
 *@version: V1.0.5
*/
export function removeUser(option) {
  return encapsulation({
    url: '/api/tcb/databasedelete',
    method: 'post',
    data: {
      query: `db.collection('boxAdministrators')
      .where({
        _id:'${option}'
      }).remove()`
    }
  })
}

